//
//  ViewController.m
//  DaoNativeSave
//
//  Created by tanqilong on 16/7/7.
//  Copyright © 2016年 tanqilong. All rights reserved.
//

#import "ViewController.h"
#import "FMDB.h"
#import <CommonCrypto/CommonDigest.h>
#import "XLDataBaseManager.h"
#import "TestDao.h"
#import "CMNet.h"

@interface ViewController ()
@property (nonatomic,strong) FMDatabase * db;
@property (nonatomic,strong) XLDataBaseManager * dataManager;
@property (nonatomic,strong) TestDao * testDao;
@property (nonatomic,strong) CMNet * net;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建数据库
//    NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"test.db"];
//    NSLog(@"地址:%@",dbPath);
//    _db = [FMDatabase databaseWithPath:dbPath];
//    if (![_db open]) {
//        NSLog(@"数据库打开失败");
//    }
//    //创建表
//    [self createTable];
    
    self.dataManager = [[XLDataBaseManager alloc]initWithDataBaseUrl:nil];
}

//插入一条数据
- (IBAction)insertDateButtonClicked:(id)sender {
    NSString * str = @"htttp://www.baidu.com?a=1,b=2123123asdfasd";
    [self.dataManager insertDataToNetTableWithUrlStr:str Value:str CompleteBlock:nil];
}

//查询数据
- (IBAction)selectDataButtonClicked:(id)sender {
    [self.dataManager selectValueDataByNetTableWithUrlStr:@"htttp://www.baidu.com?a=1,b=2123123asdfasd" CompleteBlock:^(NSString *data) {
//        NSString * valueStr = [[NSString alloc]initWithData:data encoding:kCFStringEncodingUTF8];
        NSLog(@"%@",data);
    }];
}
- (IBAction)requestTest:(id)sender {
//    _testDao = [TestDao requestNativeWithParameters:nil IndicatorView:self.view LoadingFinished:^(XLBaseNetMode *resulte) {
//        NSLog(@"%@",resulte);
//    } CancelLoading:nil LoadingError:^(id resulte) {
//        
//    }];
//    
    
    
    for (int i = 0 ; i < 20; i ++) {
        _net = [CMNet requestWithUrl:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" Parameters:nil IndicatorView:nil LoadingFinished:^(XLBaseNetMode *resulte) {
            NSLog(@"完成");
        } CancelLoading:nil LoadingError:^(id resulte) {
            
        }];
    }
    
  
    
//    _testDao = [TestDao requestWithParameters:nil IndicatorView:self.view LoadingFinished:^(XLBaseNetMode *resulte) {
//        NSLog(@"%@",resulte);
//    } CancelLoading:nil LoadingError:^(id resulte) {
//        
//    }];
    
//    _testDao = [TestDao requestNativeAndNetWithParameters:nil IndicatorView:self.view LoadingFinished:^(XLBaseNetMode *resulte) {
//        NSLog(@"%@",resulte);
//    } CancelLoading:nil LoadingError:^(id resulte) {
//        
//    }];
    
//    [TestDao requestNativeAndNetWithParameters:nil IndicatorView:self.view LoadingFinished:^(XLBaseNetMode *resulte) {
//        NSLog(@"%@",resulte);
//        self;
//    } CancelLoading:nil LoadingError:^(id resulte) {
//        
//    }];
}

//简历表格
-(void)createTable {
    if ([_db open]) {
        NSString *sqlCreateTable =  [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' (%@ INTEGER,%@ INTEGER, %@ TEXT,%@ INTEGER, PRIMARY KEY(%@,%@))",@"requestTable",@"hashCode",@"md5Code",@"pathStr",@"value",@"hashCode",@"md5Code"];
        BOOL res = [_db executeUpdate:sqlCreateTable];
        if (!res) {
            NSLog(@"error when creating db table");
        } else {
            NSLog(@"success to creating db table");
        }
        [_db close];
    }
}

//插入数据
-(void)addAdate:(NSInteger)hashCode md5Code:(NSInteger)md5Code PathStr:(NSString *)pathStr Value:(NSInteger)value {
    if ([_db open]) {
        NSString * insertStr = [NSString stringWithFormat:
                                @"INSERT INTO '%@' ('%@','%@', '%@', '%@') VALUES ('%@', '%@','%@', '%@')",
                                @"requestTable", @"hashCode",@"md5Code",@"pathStr", @"value", @(hashCode), @(md5Code),pathStr, @(value)];
        BOOL res = [_db executeUpdate:insertStr];
        if (res) {
            NSLog(@"插入数据成功");
        }
    }
}

- (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
     unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
//    return [NSString stringWithFormat:
//            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
//            result[0], result[1], result[2], result[3],
//            result[4], result[5], result[6], result[7],
//            result[8], result[9], result[10], result[11],
//            result[12], result[13], result[14], result[15]
//            ];
    return [NSString stringWithFormat:
            @"%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
